package com.jd.impl;

import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jd.dao.MatchDao;
import com.jd.dao.MessageDao;
import com.jd.dao.SignUpDao;
import com.jd.dao.TicketDao;
import com.jd.entity.MessageEntity;
import com.jd.entity.MyTicketEntity;
import com.jd.entity.SignUpEntity;
import com.jd.service.SignUpService;
import com.jd.util.IdUtil;

@Service
public class SignUpImpl implements SignUpService {

	@Autowired 
	private SignUpDao signUpDao;
	@Autowired
	private MessageDao messageDao;
	@Autowired
	private MatchDao matchDao;
	@Autowired
	private TicketDao ticketDao;

	@Override
	public String signUp(SignUpEntity signUpEntity) {
		// TODO Auto-generated method stub
		String sign="报名成功";
		//比赛详情
		HashMap<String, Object> matchContent = matchDao.getMatchContent(signUpEntity.getMatchId(), signUpEntity.getUserId());
		Integer signPerson = Integer.valueOf((String.valueOf(matchContent.get("sign_person"))));
		Integer maxPerson = Integer.valueOf((String.valueOf(matchContent.get("max_person"))));
		if(signPerson==maxPerson){
			return "该赛事已报满，看看其他的吧";
		}else{
			signPerson = signPerson+1;
		}
		//查看是否有过期的门票
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("userId", signUpEntity.getUserId());
		map.put("nameId", matchContent.get("name_id"));
		MyTicketEntity myTicketEntity = ticketDao.getTicketOut(map);
		//使用即将过期的门票
		//使用购买的门票
		MyTicketEntity ticketNull = ticketDao.getTicketNull(map);
		if(myTicketEntity==null && ticketNull==null ){
			return "您还没有该赛事门票，快去购买吧";
		}
		HashMap<String, Object> signUpMap = new HashMap<String, Object>();
		signUpMap.put("userId", signUpEntity.getUserId());
		signUpMap.put("matchId", signUpEntity.getMatchId());
		Integer count = signUpDao.countSignUp(signUpMap);
		if(count>0){
			return "您已经报过名了";
		}
		Boolean signUp = signUpDao.signUp(signUpEntity);
		if(signUp){
			//插入消息
			MessageEntity messageEntity = new MessageEntity();
			String mid=IdUtil.getIdByUUId();
			messageEntity.setId(mid);
			messageEntity.setUserId(signUpEntity.getUserId());
			messageEntity.setTitle("报名成功");
			messageEntity.setContent(matchContent.get("name")+"报名成功,请准时参加");
			messageEntity.setCreateBy(signUpEntity.getUserId());
			messageEntity.setCreateTime(new Date());
			messageEntity.setRemark1("1");
			messageEntity.setDelflag("0");
			messageDao.save(messageEntity);
			//修改报名人数
			matchContent.put("sign_person", signPerson);
			matchDao.updateSignPerson(matchContent);
			//使用门票
			if(myTicketEntity!=null && !myTicketEntity.equals("")){
				myTicketEntity.setRemark1(signUpEntity.getMatchId());
				ticketDao.updateDefalg(myTicketEntity);
			}else{
				ticketNull.setRemark1(signUpEntity.getMatchId());
				ticketDao.updateDefalg(ticketNull);
			}
			return "报名成功";
		}
		//报名
		return sign;
	}

	@Override
	public Boolean updateStart(SignUpEntity signUpEntity) {
		// TODO Auto-generated method stub
		return signUpDao.updateStart(signUpEntity);
	}

	@Override
	public SignUpEntity getSignUp(SignUpEntity signUpEntity) {
		// TODO Auto-generated method stub
		return signUpDao.getSignUp(signUpEntity);
	}
}
